from django.apps import AppConfig
from django.db.models.signals import post_migrate
from django.contrib.auth.hashers import make_password


def create_admin_user(sender, **kwargs):
    from .models import SysUser

    # 检查是否已经存在管理员用户
    if not SysUser.objects.filter(username='admin').exists():
        SysUser.objects.create(
            username='admin',
            nickname='管理员',
            password='111111',  # 普通密码
            avatar='https://img.picgo.net/2025/01/23/22b6fcf2-21d1-4ea6-b4d5-aa139b4f7bce69343d552b0a14f0.jpg'  # 默认头像
        )
        print("管理员用户已创建")
    else:
        print("管理员用户已存在，无需创建")


class UserConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'user'

    def ready(self):
        # 连接信号
        post_migrate.connect(create_admin_user, sender=self)
